package com.example.Util;

import com.aliyun.tea.TeaException;
import lombok.Data;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Random;

@Component
@ConfigurationProperties(prefix = "aliyun.oss")
@Data
public class SampleUtil {

    private   String endpoint;

    private   String bucketName;

    private   String accessKeyId;

    private    String accessKeySecret;


        public   com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                    .setAccessKeyId(accessKeyId)
                    .setAccessKeySecret(accessKeySecret);
            config.endpoint = "dysmsapi.aliyuncs.com";
            return new com.aliyun.dysmsapi20170525.Client(config);
        }
        public      void sendSms(String phoneNumber, String verificationCode) throws Exception {
            com.aliyun.dysmsapi20170525.Client client = createClient();
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setSignName("ziyilin")
                    .setTemplateCode("SMS_468380721")
                    .setPhoneNumbers(phoneNumber)
                    .setTemplateParam("{\"code\":\"" + verificationCode + "\"}");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            try {
                client.sendSmsWithOptions(sendSmsRequest, runtime);
            } catch (TeaException error) {
                System.out.println(error.getMessage());
                System.out.println("+++++++++++++");
                com.aliyun.teautil.Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                System.out.println(error.getMessage());
                com.aliyun.teautil.Common.assertAsString(error.message);
            }
        }


        //随机生成四位数字验证码
    public  String generateRandomCode() {
        Random random = new Random();
        int code = 1000 + random.nextInt(9000); // 生成 1000 到 9999 之间的随机数
        System.out.println(accessKeyId);
        return String.valueOf(code);
    }

}
